﻿using CloudDcPc.Entity;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudDcPc.Bean
{
    public class MenuTypeBean
    {
        private MySqlConnection myConn = null;
        private DBConnection dbc = null;
        private MySqlCommand cmd = null;
        public MenuTypeBean()
        {
            this.dbc = new DBConnection();
            this.myConn = this.dbc.getConnection();
        }
        public bool AddMenuType(MenuType menuType)
        {
            string sql = "insert into dc_menutype(name,hykcanuse,djqcanuse) values('" + menuType.Name + "','" + menuType.Hykcanuse + "','" + menuType.Djqcanuse + "')";
            try
            {
                if (myConn.State != System.Data.ConnectionState.Closed) System.Threading.Thread.Sleep(1000); myConn.Open();
                cmd = new MySqlCommand(sql, myConn);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                dbc.close();
            }
        }

        public bool UpdateMenuType(MenuType menuType)
        {
            string sql = "update dc_menutype set name='" + menuType.Name + "',hykcanuse='" + menuType.Hykcanuse + "',djqcanuse='" + menuType.Djqcanuse + "' where id='" + menuType.Id + "'";
            try
            {
                if (myConn.State != System.Data.ConnectionState.Closed) System.Threading.Thread.Sleep(1000); myConn.Open();
                cmd = new MySqlCommand(sql, myConn);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                dbc.close();
            }

        }

        public bool DelMenuTypeById(int id)
        {
            string sql = "delete from dc_menutype where id='" + id + "'";
            try
            {
                if (myConn.State != System.Data.ConnectionState.Closed) System.Threading.Thread.Sleep(1000); myConn.Open();
                cmd = new MySqlCommand(sql, myConn);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                dbc.close();
            }
        }

        public ObservableCollection<MenuType> GetAllMenuType()
        {
            ObservableCollection<MenuType> list = new ObservableCollection<MenuType>();
            MenuType menuType = null;
            string sql = "select * from dc_menutype";
            try
            {
                if (myConn.State != System.Data.ConnectionState.Closed) System.Threading.Thread.Sleep(1000); myConn.Open();
                cmd = new MySqlCommand(sql, myConn);
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    menuType = new MenuType()
                    {
                        Id = (int)reader["id"],
                        Name = reader["name"].ToString(),
                        Djqcanuse = (int)reader["djqcanuse"],
                        Hykcanuse = (int)reader["hykcanuse"]
                    };
                    list.Add(menuType);
                }

            }
            finally
            {
                dbc.close();
            }
            return list;
        }

        public List<MenuType> GetMenuTypes()
        {
            List<MenuType> list = new List<MenuType>();
            string sql = "select * from dc_menutype";
            MenuType menuType = null;
            try
            {
                if (myConn.State != System.Data.ConnectionState.Closed) System.Threading.Thread.Sleep(1000); myConn.Open();
                cmd = new MySqlCommand(sql, myConn);
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    menuType = new MenuType();
                    menuType.Id = (int)reader["id"];
                    menuType.Name = reader["name"].ToString();
                    menuType.Djqcanuse = (int)reader["djqcanuse"];
                    menuType.Hykcanuse = (int)reader["hykcanuse"];
                    list.Add(menuType);
                }
                return list;
            }
            finally
            {
                dbc.close();
            }
        }
    }
}
